package com.returnsoft.resource.eao;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import com.returnsoft.resource.entity.Area;
import com.returnsoft.resource.exception.EaoExcepcion;

@LocalBean
@Stateless
public class AreaEao {

	@PersistenceContext
	private EntityManager em;

	public List<Area> findAreasParent() throws EaoExcepcion {
		try {
			String query = "SELECT a FROM Area a left join a.area ac where ac.id is null";
			TypedQuery<Area> q = em.createQuery(query, Area.class);

			List<Area> areas = q.getResultList();

			return areas;
		} catch (Exception e) {
			e.printStackTrace();
			throw new EaoExcepcion(e.getMessage());
		}
	}

	public List<Area> findAreasChild(Integer areaId) throws EaoExcepcion {
		try {
			String query = "SELECT a FROM Area a left join a.area ac where ac.id = :areaId";
			TypedQuery<Area> q = em.createQuery(query, Area.class);
			q.setParameter("areaId", areaId);
			List<Area> areas = q.getResultList();
			return areas;
		} catch (Exception e) {
			e.printStackTrace();
			throw new EaoExcepcion(e.getMessage());
		}
	}

	public List<Area> findByRecruiter(Integer recruiterId) throws EaoExcepcion {
		try {
			String query = "SELECT a " + "FROM Area a "
					+ "left join a.recruiters r " + "where r.id = :recruiterId";
			TypedQuery<Area> q = em.createQuery(query, Area.class);
			q.setParameter("recruiterId", recruiterId);
			List<Area> areas = q.getResultList();
			return areas;
		} catch (Exception e) {
			e.printStackTrace();
			throw new EaoExcepcion(e.getMessage());
		}
	}

	public List<Area> findByTrainer(Integer trainerId) throws EaoExcepcion {
		try {
			String query = "SELECT a " + "FROM Area a "
					+ "left join a.trainers r " + "where r.id = :trainerId";
			TypedQuery<Area> q = em.createQuery(query, Area.class);
			q.setParameter("trainerId", trainerId);
			List<Area> areas = q.getResultList();
			return areas;
		} catch (Exception e) {
			e.printStackTrace();
			throw new EaoExcepcion(e.getMessage());
		}
	}

}
